// askimage start
function changeSelectionImgSrc(imgSrc, imgOnerror) {
	var selectImg = $(".ask_pop_upimg_image");
	if (selectImg.find("img").length == 0) {
		selectImg.html("<img src=''/>");
	}
	selectImg.find("img").attr("src", imgSrc);
	selectImg.find("img").attr("onerror", imgOnerror);
	$(".ask_pop_manmge dl dt a").addClass("ask_pop_yesbtn");
}

function askImagePrevPage() {
	var params = $(".ask_pop_searchimg").data("imageQuery");
	if (params['imageQuery.toPage'] <= 1) {
		$(this).removeAttr("href");
	} else {
		params['imageQuery.toPage'] -= 1;
		askImage(params);
	}
	$("#c1-grid .c1-page-prev").attr("href", "javascript:");
	$(".c1-page-count span").html(params['imageQuery.toPage']);
}
function askImageNextPage() {
	var params = $(".ask_pop_searchimg").data("imageQuery");
	params['imageQuery.toPage'] += 1;
	askImage(params);
	$("#c1-grid .c1-page-prev").attr("href", "javascript:");
	$(".c1-page-count span").html(params['imageQuery.toPage']);
}

function askImageInit() {
	var obj = $(".ask_pop_searchimg");
	var searchKey = $.trim(obj.val());
	if (isUndefined(searchKey)) {
		searchKey = $.trim($(".ask_pop_txt").val());
	}
	var params = {
		"imageQuery.imageSearchKey" : searchKey,
		"imageQuery.toPage" : 1
	};
	obj.data("imageQuery", params);
	var url = "../ask/bingImageSearch.action";
	doAjaxHtml(url, params, askImageSuc);
}

function askImage(params) {
	if (isUndefined(params["imageQuery.imageSearchKey"])) {
		return false;
	}
	var url = "../ask/bingImageSearch.action";
	doAjaxHtml(url, params, askImageSuc);
}

function askImageSuc(response) {
	$("#c1-grid ul").html(response);
	regBingSearch();
	var firstImage = $("#c1-grid ul a").get(0);
	if(firstImage){
		firstImage.click();
	}
}

function regBingSearch() {
	$("#c1-grid ul a").click(function() {
				$("#c1-grid ul li").removeClass("c1-selected");
				var imga = $(this);
				imga.parent().addClass("c1-selected");
				var img = imga.find("img");
				changeSelectionImgSrc(img.attr("src"), img.attr("onerror"));
			});
}
// askimage end

// liuwenlong begin/////////////////////////////////

function askInit() {
	var url = "../ask/askInit.action";
	var params = {};
	doAjaxHtml(url, params, function(response) {
				$(".ask_pop").remove();
				$("body").append(response);
				$('input[type="file"]').css('opacity', 0);
				txtFocus($('.ask_pop .ask_pop_txt'));
				txtFocus($('.ask_pop .ask_pop_searchimg'));
				txtDefaultvalue($('.ask_pop .ask_pop_txt'),
						$('.ask_pop .ask_pop_default'));
				textSize($('.ask_pop .ask_pop_txt'),
						$('.ask_pop .txt_count span'),
						$('.ask_pop .txt_count strong'));
				addAskImg();
				var clickItShow = $('.ask_pop');
				popPosition(clickItShow);
				clickItShow.show();
				appendCover();
				var shut = $('.ask_pop .shut');
				shut.click(function() {
							clickItShow.hide();
							removeCover();
						});
			});
};

function createAsk() {
	var createAskBtn = $(this);
	if (!createAskBtn.hasClass("ask_pop_yesbtn")) {
		return;
	}
	createAskBtn.removeClass("ask_pop_yesbtn");
	var askQues = $.trim($(".ask_pop_txt").val());
	var askPicPath = $.trim($(".ask_pop_upimg_image img").attr("onerror"));
	var url = "../askAjax/createAsk.action";
	var params = {
		"query.question" : askQues,
		"query.picPath" : askPicPath,
		"query.askType" : 1,
		"query.preventOther" : $(".ask_pop_manmge input[type='checkbox']")
				.attr("checked") ? 0 : 1
	};
	doAjax(url, params, function(ajaxResult) {
				if (ajaxResult.OK) {
					alertTipDown("发问问成功", "dui");
					location.href = "http://yoasking.com";
				} else {
					alertTipDown("发问问失败", "cuo");
				}
				createAskBtn.addClass("ask_pop_yesbtn");
			});
}

// liuwenlong end ////////////////////////////////
function uploadPicture() {
	var options = {
		url : "../image/uploadPic.action",
		type : "POST",
		dataType : "script",
		success : function(msg) {
			if (msg.indexOf("#") > 0) {
				var data = msg.split("#");
				changeSelectionImgSrc(data[1].split("</pre>")[0], data[1]
								.split("</pre>")[0]);
			}
		},
		error : function(e) {
			var msg = e.responseText;
			if (msg.indexOf("#") > 0) {
				var data = msg.split("#");
				changeSelectionImgSrc(data[1].split("</pre>")[0], data[1]
								.split("</pre>")[0]);
			}
		}
	};
	$("#askImageForm").ajaxSubmit(options);
	return false;
}
// 第一次
function findFollowAsk() {
	clearStreaml();
	$(this).addClass("cur").parent().siblings().children('a')
			.removeClass("cur");
	var url = "../ask/findFollowAsk.action";
	findAskList(url);
}
function findLatestAsk() {
	clearStreaml();
	$(this).addClass("cur").parent().siblings().children('a')
			.removeClass("cur");
	var url = "../ask/findLatestAsk.action";
	findAskList(url);
}
function findHotAsk() {
	clearStreaml();
	$(this).addClass("cur").parent().siblings().children('a')
			.removeClass("cur");
	var url = "../ask/findHotAsk.action";
	findAskList(url);
}

function clearStreaml() {
	var streaml = $("#streamList");
	$("#streamList .stream-col").html("");
	streaml.attr("isEnd", "0");
	streaml.attr("toPage", "1");
	streaml.attr("lastAskId", "");

}
// load
function loadFollowAsk() {
	var url = "../ask/findFollowAsk.action";
	findAskList(url);
}
function loadLatestAsk() {
	var url = "../ask/findLatestAsk.action";
	findAskList(url);
}
function loadHotAsk() {
	var url = "../ask/findHotAsk.action";
	findAskList(url);
}
function loadNoLoginUserAsk(){
	
}
function findAskList(url) {
	var streaml = $("#streamList");
	if (streaml.attr("isEnd") == "1") {
		alertTipDown("没有了!", "cuo");
		return;
	}
	var toPage = parseInt(streaml.attr("toPage"));
	var params = {
		"query.toPage" : toPage,
		"query.lastAskId" : streaml.attr("lastAskId")
	};
	doAjaxHtml(url, params, function(response) {
				var resultObjAsk = $(response);
				if ($.trim(resultObjAsk.html()) == "") {
					return false;
				}
				var totalPage = parseInt(resultObjAsk.attr("totalPage"));
				if (toPage == totalPage) {
					streaml.attr("isEnd", "1");
				} else {
					streaml.attr("toPage", toPage + 1);
				}
				var askList = resultObjAsk.find(".stream-one");
				var askIdArr = [];
				var streamCol1 = streaml.find(".stream-col:eq(0)");
				var streamCol2 = streaml.find(".stream-col:eq(1)");
				var streamCol3 = streaml.find(".stream-col:eq(2)");
				for (var i = 0; i < askList.length; i++) {
					if (i % 3 == 0) {
						streamCol1.append(askList.eq(i));
					} else if (i % 3 == 1) {
						streamCol2.append(askList.eq(i));
					} else if (i % 3 == 2) {
						streamCol3.append(askList.eq(i));
					}
					askIdArr.push(askList.eq(i).attr("id"));
				}
				if (toPage == 1 && askIdArr.length > 0) {
					streaml.attr("lastAskId", askIdArr[0].substring(4));
				}
				askListReg(askIdArr);
				var loadTag = $('<div class="loadTag" />').appendTo($('body'));
				viewUserInfo();
			});
}
function askListReg(askIdArr) {
	for (var i = 0; i < askIdArr.length; i++) {
		var ask = $("#" + askIdArr[i]);
		// like
		ask.find(".stream-stat-likes a").click(addLike);
		ask.find("textarea").keypress(addAnswer);
		ask.find(".stream-vote-trigger").click(answerVote);
		answerAnimate(askIdArr[i].substring(4));
		txtFocus(ask.find('.stream-expander textarea'));
		textSize(ask.find('.stream-expander textarea'), ask
						.find('.stream-expander .curSize'), ask
						.find('.stream-expander .maxSize'));
		txtDefaultvalue(ask.find('.stream-expander textarea'), ask
						.find('span.defaultT'));
	}
}
// 喜欢
function addLike() {
	var like = $(this);
	var url = "../likeAjax/addLike.action";
	var params = {
		"like.askId" : like.parents(".yoask").attr("askId")
	};
	doAjax(url, params, function(ajaxResult) {
				if (ajaxResult.OK) {
					like.addClass("stream-liker");
					like.unbind().click(deleteLike);
					var likeCount = like.parent().find("span");
					likeCount.text(parseInt(likeCount.text()) + 1);
				} else {
					alertTipDown(ajaxResult.message, "cuo");
				}
			});
}
// 取消喜欢
function deleteLike() {
	var like = $(this);
	var url = "../likeAjax/deleteLike.action";
	var params = {
		"like.askId" : like.parents(".yoask").attr("askId")
	};
	doAjax(url, params, function(ajaxResult) {
				if (ajaxResult.OK) {
					like.removeClass("stream-liker");
					like.unbind().click(addLike);
					var likeCount = like.parent().find("span");
					likeCount.text(parseInt(likeCount.text()) - 1);
				} else {
					alertTipDown(ajaxResult.message, "cuo");
				}
			});
}
// 添加观点
function addAnswer(e) {
	var answer = $(this);
	if (e.keyCode != 13) {
		return;
	} else {
		// answer.val("");
	}
	var answerBody = $.trim(answer.val());
	if (isUndefined(answerBody)) {
		alert("你的观点是？？？");
		return;
	}
	var url = "../answerAjax/addAnswer.action";
	var askId = answer.parents(".yoask").attr("askId");
	var params = {
		"query.askId" : askId,
		"query.answerBody" : answerBody
	};
	doAjax(url, params, function(ajaxResult) {
				answer.val("");
				if (ajaxResult.OK) {
					$('#addAnswerDiv .txt_count span').text(0);
					answer.next().find(".curSize").text(0);
					getVoteRate(askId);
				} else {
					alertTipDown(ajaxResult.message, "cuo");
					alertTipUp();
				}
			});
}
// 投票
function answerVote() {
	var answerVote = $(this);
	if (answerVote.attr("href") != "###") {
		return;
	}
	var url = "../answerVoteAjax/addVote.action";
	var askId = answerVote.parents(".yoask").attr("askId");
	var answerId = answerVote.attr("answerId");
	var params = {
		"vote.askId" : askId,
		"vote.answerId" : answerId
	};
	doAjax(url, params, function(ajaxResult) {
				if (ajaxResult.OK) {
					getVoteRate(askId, answerId);
				} else {
					alertTipDown(ajaxResult.message, "cuo");
				}
			});
}

function getVoteRate(askId) {
	var url = "../answer/getVoteRate.action";
	var params = {
		"query.askId" : askId
	};
	doAjaxHtml(url, params, function(response) {
				$("#ask_" + askId + " .stream-options").html(response);
				$("#ask_" + askId + " .stream-vote-trigger").click(answerVote);
				answerAnimate(askId);
			});
}

function answerAnimate(askId) {
	$("#ask_" + askId + " .stream-options li").each(function() {
				var obj = $(this).find(".stream-option-bar");
				var width = obj.attr("voteRate") + "%";
				obj.animate({
							"width" : width
						});
			});
}

// 个人页
function findUserAskList(url) {
	var userId = $("#myId").attr("value");
	var streaml = $("#streamList");
	if (streaml.attr("isEnd") == "1") {
		alertTipDown("没有了!", "cuo");
		return;
	}
	var toPage = 1;
	if (!isUndefined(streaml.attr("toPage"))) {
		toPage = parseInt(streaml.attr("toPage"));
	}
	var params = {
		"query.userId" : userId,
		"query.toPage" : toPage,
		"query.lastAskId" : streaml.attr("lastAskId")
	};
	doAjaxHtml(url, params, function(response) {
				var resultObjAsk = $(response);
				if ($.trim(resultObjAsk.html()) == "") {
					return false;
				}
				var totalPage = parseInt(resultObjAsk.attr("totalPage"));
				if (toPage == totalPage) {
					streaml.attr("isEnd", "1");
				} else {
					streaml.attr("toPage", toPage + 1);
				}
				var askList = resultObjAsk.find(".stream-one");
				var askIdArr = [];
				var streamCol1 = streaml.find(".stream-col:eq(0)");
				var streamCol2 = streaml.find(".stream-col:eq(1)");
				var streamCol3 = streaml.find(".stream-col:eq(2)");
				for (var i = 0; i < askList.length; i++) {
					if (i % 3 == 0) {
						streamCol1.append(askList.eq(i));
					} else if (i % 3 == 1) {
						streamCol2.append(askList.eq(i));
					} else if (i % 3 == 2) {
						streamCol3.append(askList.eq(i));
					}
					askIdArr.push(askList.eq(i).attr("id"));
				}
				if (toPage == 1) {
					streaml.attr("lastAskId", askIdArr[0].substring(4));
				}
				askListReg(askIdArr);
				var loadTag = $('<div class="loadTag" />').appendTo($('body'));

			});
}
function findLoginUserAsk(){
	var url = "../ask/findUserAsk.action";
	findUserAskList(url);
}
function findNoLoginUserAsk(){
	var streaml = $("#streamList");
	if(streaml.attr("isEnd")=="1"){
		alertTipDown("没有了!", "cuo");
		return;
	}
	var url = "../ask/findNoLoginUserAsk.action";
	var userId = $("#myId").attr("value");
	var toPage = 1;
	if (!isUndefined(streaml.attr("toPage"))) {
		toPage = parseInt(streaml.attr("toPage"));
	}
	var params = {
		"query.userId" : userId,
		"query.toPage" : toPage,
		"query.lastAskId" : streaml.attr("lastAskId")
	};
	doAjaxHtml(url, params, function(response) {
				var resultObjAsk = $(response);
				if ($.trim(resultObjAsk.html()) == "") {
					return false;
				}
				var totalPage = parseInt(resultObjAsk.attr("totalPage"));
				if (toPage == totalPage) {
					streaml.attr("isEnd", "1");
				} else {
					streaml.attr("toPage", toPage + 1);
				}
				var askList = resultObjAsk.find(".stream-one");
				var askIdArr = [];
				var streamCol1 = streaml.find(".stream-col:eq(0)");
				var streamCol2 = streaml.find(".stream-col:eq(1)");
				var streamCol3 = streaml.find(".stream-col:eq(2)");
				for (var i = 0; i < askList.length; i++) {
					if (i % 3 == 0) {
						streamCol1.append(askList.eq(i));
					} else if (i % 3 == 1) {
						streamCol2.append(askList.eq(i));
					} else if (i % 3 == 2) {
						streamCol3.append(askList.eq(i));
					}
					askIdArr.push(askList.eq(i).attr("id"));
				}
				if (toPage == 1) {
					streaml.attr("lastAskId", askIdArr[0].substring(4));
				}
				popPub($("#streamList a").not(".askImg").not(".userImg"),$('#pop_login'),$('#pop_login h3 a'));
				popPub($(".stream-expander textarea"),$('#pop_login'),$('#pop_login h3 a'));
				var loadTag = $('<div class="loadTag" />').appendTo($('body'));

			});
}